package org.robot.work;

import java.util.List;

import org.jsoup.helper.StringUtil;
import org.robot.Spider;
import org.robot.component.Page;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Author;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.MatchEmail;
import org.robot.utils.RegexUtil;

/**
 * 被封
 * @author pdz
 * @date 2019年1月10日
 */
public class Annualreviews extends Spider {
	private static final Long TASKID = 68913L;

	@Override
	public void parse(Page page) throws Exception {
		if (page.typeEquals("journal")) {
//			parseJournal(page);
			page.setNextLinksFilter(e -> {
				String year = RegexUtil.getYear(e.text());
				return !StringUtil.isBlank(year) && year.matches("201[7-9]");
			});
		} /*else if (page.typeEquals("issue")) {
			parseIssue(page);
		} else if (page.typeEquals("paper"))
			parsePaper(page);*/
	}
	

	private void parsePaperInfo(Page page) {
		List<String> list = page.select("meta[name=DC.Contributor]").eachAttr("content");
		String[] authors = list.toArray(new String[list.size()]);
		String email = page.select(".em-addr").text().replace("{at}", "@");
		String matchAuthor = MatchEmail.similarName(authors, email);
		Long paperid = PaperDao.selectPidByWebsite(Paper.class, page.prevUrl(), TASKID);
		for (String aut : authors) {
			Author author = new Author();
			author.setFullName(aut);
			author.setTaskId(TASKID);
			author.setPaperid(paperid);
			if (aut.equals(matchAuthor))
				author.setEmail(email);
//			AuthorDao.addAuthor(author);
		}
	}


	private void parsePaper(Page page) {
		String doi = page.select("meta[name=DC.Identifier]").attr("content");
		String abst = page.select("meta[name=DC.Description]").attr("content");
		Long issueid = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(), TASKID);
		
		Paper paper = new Paper();
		paper.setWebsite(page.url());
		paper.setTitle(page.urlText());
		paper.setTaskid(TASKID);
		paper.setDoi(doi);
		paper.setAbstracts(abst);
		paper.setPdfUrl(page.url() + ".full.pdf");
		paper.setIssueId(issueid);
//		PaperDao.addPaper(paper);
	}


	private void parseJournal(Page page) {
		String str = page.select(".snippet-content").text();
		String issn = RegexUtil.getIssn(str);
		String name = page.select("title").text().split("|")[1].trim();
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn(issn);
		journal.setTaskId(TASKID);
		journal.setJournalname(name);
//		JournalDao.addJournal(journal);
	}


	private void parseIssue(Page page) {
		String str = page.urlText();
		String issueVal = str.substring(0, str.lastIndexOf(":")).trim();
		String year = RegexUtil.regexFirst("2\\d{3}", issueVal);
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(3), TASKID);
		
		Issue issue = new Issue();
		issue.setIssue(issueVal);
		issue.setYear(year);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
//		IssueDao.addIssue(issue);
	}


	public static void main(String[] args) {
		Annualreviews claw = new Annualreviews();
		claw.setBaseDirectory("E:/webSpider/Annualreviews");
//		claw.getConfig().setInterval(10);
		claw.addSeed("https://www.annualreviews.org/action/showPublications");
		claw.addRegex("https://www.annualreviews.org/journal/[a-z]+", "journal");
		claw.addRegex("https://www.annualreviews.org/toc/[a-z]+/\\d+/\\d+","issue");
		claw.addRule(".teaser .row .text a", "https://www.annualreviews.org/doi/abs/.+", "paper");
		claw.start();
		
	}

}
